import { Switch, withRouter } from 'react-router-dom'
import { connect } from 'react-redux'

import router from './router'

import RouterGuard from './components/routerGuard'
import { useEffect } from 'react'
import { exitSocket, getSocket, offQuitLog, onQuitLog } from './socketEvent'
import { message } from 'antd'
import { changeLog, changeUserInfo } from './store/actions'


function App(props) {
	// 判断是否移动端
	// const [isMobile, setIsMobile] = useState(false);
	// const ua = navigator.userAgent.toLowerCase();
	// const agents = ['iphone', 'ipad', 'ipod', 'android', 'linux', 'windows phone'];
	// useEffect(() => {
	// 	for (let i = 0; i < agents.length; i++) {
	// 		if (ua.includes(agents[i])) {
	// 			console.log(ua.includes(agents[i]))
	// 			setIsMobile(true);
	// 			break;
	// 		}
	// 	}
	// }, []);

	
	const { islogin, history, changeLog, changeUserInfo } = props;
	useEffect(() => {
		if (islogin) {
			onQuitLog(res => {
				message.error({content: res.message});
				changeLog(false);
				changeUserInfo();
				exitSocket();
				history.push({pathname: '/home'});
			});
		}
		return () => {
			offQuitLog();
		}
	}, [islogin, history, changeLog, changeUserInfo]);



	return (
		<Switch>
			<RouterGuard routerObj={router} />
		</Switch>
	)
}

function mapStatetoProps(state) {
	return {
		islogin: state.islogin,
	}
}

export default connect(
	mapStatetoProps, 
	{
		changeLog,
		changeUserInfo,
	}
)(withRouter(App));